/*
 * @Author: Bowen 849084209@qq.com
 * @Date: 2023-02-07 21:47:11
 * @LastEditors: Bowen 849084209@qq.com
 * @LastEditTime: 2023-02-13 20:58:10
 * @FilePath: \ming\demo1\src\main\java\com\ming\demo1\controller\OpenApiController.java
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
package com.ming.demo1.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.ming.ming.MingConfig;

import net.lvsq.jgossip.model.GossipMember;

/**
 * 接口
 * 
 * @author Bowen
 *
 */
@RestController
@RequestMapping("/")
public class OpenApiController {
	
	@Autowired
	private MingConfig mingConfig;
	
	/**
	 * 询问谁是老大
	 * @return
	 */
	@GetMapping("leader")
	public Integer getLeader(){
		return mingConfig.getLeaderId();
	}
	
	/**
	 * 模拟服务宕机下线
	 * @return
	 */
	@GetMapping("stop")
	public String stop(){
		mingConfig.getGossipService().shutdown();
		System.exit(0);
		return "demo1";
	} 
	
	/**
	 * 发送数据
	 * @param msg
	 * @return
	 */
	@GetMapping("send")
	public String send(@RequestParam String msg){
		System.out.println("准备发送:"+msg);
		mingConfig.getGossipService().getGossipManager().publish(msg);
		return "demo1";
	} 
	
	/**
	 * 模拟业务接口
	 * @return
	 */
	@GetMapping("/hello")
	public String hello(){
		System.out.println("模拟业务接口，");
		return "我是 8081 ，你好大兄弟！";
	} 
	
	/**
	 * 查询当前有谁在线
	 * 谁是leader
	 * @return
	 */
	@GetMapping("/onLive")
	public List<GossipMember> onLive(){
		List<GossipMember> listM = mingConfig.getGossipService().getGossipManager().getLiveMembers();
		System.out.println("我是8081 ，我看看谁在线"+listM.toString());
		return listM;
	} 
	
}
